<template>
  <el-dialog title="库存物资选择"
    v-if="showFlag"
    :visible.sync="showFlag"
    :modal= false
    width="90%"
    center
  >
    <el-row :gutter="20">
      <el-col :span="6" :xs="24">
        <div class="head-container">
          <el-scrollbar style="height: 600px; max-height: 70vh;">
            <el-tree
              :data="itemTypeOptions"
              :props="defaultProps"
              :expand-on-click-node="false"
              ref="tree"
              default-expand-all
              @node-click="handleNodeClick"
              class="material-tree"
            />
          </el-scrollbar>
        </div>
      </el-col>
      <el-col :span="18" :xs="24">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
          <el-form-item label="产品物料编码" prop="itemCode">
            <el-input
              v-model="queryParams.itemCode"
              placeholder="请输入产品物料编码"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="产品物料名称" prop="itemName">
            <el-input
              v-model="queryParams.itemName"
              placeholder="请输入产品物料名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="入库批次号" prop="batchCode">
            <el-input
              v-model="queryParams.batchCode"
              placeholder="请输入入库批次号"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="仓库名称" prop="warehouseName">
            <el-input
              v-model="queryParams.warehouseName"
              placeholder="请输入仓库名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="供应商编号" prop="vendorCode">
            <el-input
              v-model="queryParams.vendorCode"
              placeholder="请输入供应商编号"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="供应商名称" prop="vendorName">
            <el-input
              v-model="queryParams.vendorName"
              placeholder="请输入供应商名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="库存有效期" prop="expireDate">
            <el-date-picker clearable
              v-model="queryParams.expireDate"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="请选择库存有效期">
            </el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">    
          <el-col :span="1.5">
            <el-button
              type="warning"
              plain
              icon="el-icon-download"
              size="mini"
              @click="handleExport"
              v-hasPermi="['mes:wm:wmstock:edit']"
            >导出</el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="wmstockList" @selection-change="handleSelectionChange" 
                  style="width: 100%;">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="产品物料编码" width="120" align="center" prop="itemCode" />
          <el-table-column label="产品物料名称" width="150" align="center" prop="itemName" :show-overflow-tooltip="true"/>
          <el-table-column label="规格型号" width="120" align="center" prop="specification" :show-overflow-tooltip="true"/>
          <el-table-column label="单位" width="80" align="center" prop="unitOfMeasure" />
          <el-table-column label="入库批次号" width="100" align="center" prop="batchCode" :show-overflow-tooltip="true"/>
          <el-table-column label="仓库" width="120" align="center" prop="warehouseName" />
          <el-table-column label="库区" width="120" align="center" prop="locationName" />
          <el-table-column label="库位" width="120" align="center" prop="areaName" />
          <el-table-column label="供应商编号" width="100" align="center" prop="vendorCode" />
          <el-table-column label="供应商名称" width="120" align="center" prop="vendorName" :show-overflow-tooltip="true"/>
          <el-table-column label="供应商简称" width="100" align="center" prop="vendorNick" />
          <el-table-column label="在库数量" width="100" align="center" prop="quantityOnhand" />
          <el-table-column label="库存有效期" width="120" align="center" prop="expireDate">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.expireDate, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
        </el-table>
      
        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
          :page-sizes="[10, 20, 30]"
        />
      </el-col>
    </el-row>
    <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="confirmSelect">确 定</el-button>
        <el-button @click="showFlag=false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { listWmstock, getWmstock, delWmstock, addWmstock, updateWmstock } from "@/api/mes/wm/wmstock";
import { treeselect } from "@/api/mes/md/itemtype";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  name: "WmstockMultiSelect",
  components: { Treeselect },
  props: {
    vendorId: null,
    batchCode: null,
    warehouseId: null,
    warehouseCode: null,
    locationId: null,
    locationCode: null,
    areaId: null,
    areaCode: null,
    workorderId: null,
    itemId: null,
    itemCode: null,
    materialStockIds: null,
  },
  data() {
    return {
      showFlag: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      selectedRows: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 物料产品分类树
      itemTypeOptions: undefined,
      // 库存记录表格数据
      wmstockList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 树形组件属性
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        itemTypeId: null,
        itemId: this.itemId,
        itemCode: this.itemCode,
        itemName: null,
        specification: null,
        unitOfMeasure: null,
        batchCode: this.batchCode,
        warehouseId: this.warehouseId,
        warehouseCode: this.warehouseCode,
        warehouseName: null,
        locationId: this.locationId,
        locationCode: this.locationCode,
        locationName: null,
        areaId: this.areaId,
        areaCode: this.areaCode,
        areaName: null,
        vendorId: this.vendorId,
        vendorCode: null,
        vendorName: null,
        vendorNick: null,
        quantityOnhand: null,
        expireDate: null,
      },
      // 表单参数
      form: {},
    };
  },
  created() {
    this.getList();
    this.getTreeselect();
  },
  watch: {
    // 监听props变化，更新查询参数
    itemId: {
      handler(newVal) {
        if (newVal !== undefined) {
          this.queryParams.itemId = newVal;
        }
      },
      immediate: true
    },
    itemCode: {
      handler(newVal) {
        if (newVal !== undefined) {
          this.queryParams.itemCode = newVal;
        }
      },
      immediate: true
    },
    batchCode: {
      handler(newVal) {
        if (newVal !== undefined) {
          this.queryParams.batchCode = newVal;
        }
      },
      immediate: true
    },
    warehouseId: {
      handler(newVal) {
        if (newVal !== undefined) {
          this.queryParams.warehouseId = newVal;
        }
      },
      immediate: true
    },
    warehouseCode: {
      handler(newVal) {
        if (newVal !== undefined) {
          this.queryParams.warehouseCode = newVal;
        }
      },
      immediate: true
    },
    locationId: {
      handler(newVal) {
        if (newVal !== undefined) {
          this.queryParams.locationId = newVal;
        }
      },
      immediate: true
    },
    areaId: {
      handler(newVal) {
        if (newVal !== undefined) {
          this.queryParams.areaId = newVal;
        }
      },
      immediate: true
    },
    vendorId: {
      handler(newVal) {
        if (newVal !== undefined) {
          this.queryParams.vendorId = newVal;
        }
      },
      immediate: true
    }
  },
  methods: {
    /** 查询库存记录列表 */
    getList() {
      this.loading = true;
      listWmstock(this.queryParams).then(response => {
        this.wmstockList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询分类下拉树结构 */
    getTreeselect() {
      treeselect().then(response => {
        this.itemTypeOptions = response.data;
      });
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.itemTypeId = data.id;
      this.handleQuery();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    
    // 重置表单
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    
    // 导出功能
    handleExport() {
      this.download('wm/wmstock/export', {
        ...this.queryParams
      }, `wmstock_${new Date().getTime()}.xlsx`)
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.materialStockId)
      this.selectedRows = selection
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    
    // 确定选中
    confirmSelect(){
        if(this.selectedRows.length === 0){
            this.$notify({
                title:'提示',
                type:'warning',
                message: '请至少选择一条数据!'
            });
            return;
        }
        this.$emit('onSelected', this.selectedRows);
        this.showFlag = false;
    },

  }
};
</script>

<style scoped>
.material-tree {
  padding: 10px;
}

.material-tree .el-tree-node__content {
  height: 32px;
  line-height: 32px;
}

.material-tree .el-tree-node__label {
  font-size: 14px;
}

.head-container {
  margin-bottom: 15px;
}

.el-scrollbar {
  border: none;
  border-radius: 0;
  background-color: transparent;
}

.el-table {
  border: 1px solid #e4e7ed;
  border-radius: 4px;
}

.el-form {
  background-color: #f8f9fa;
  padding: 15px;
  border-radius: 4px;
  margin-bottom: 15px;
}
</style>
